<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
            <el-form-item label="设备分类名称" prop="typeName">
                <el-input
                v-model="queryParams.typeName"
                placeholder="请输入设备分类名"
                clearable
                style="width: 240px"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >

            <el-table-column type="selection" width="55"></el-table-column>

            <el-table-column label="编号" width="120">
                <template slot-scope="scope">{{ scope.row.typeId }}</template>
            </el-table-column>
            <el-table-column label="类别名称" width="120">
                <template slot-scope="scope">{{ scope.row.typeName }}</template>
            </el-table-column>
            <el-table-column label="类别说明" width="120">
                <template slot-scope="scope">{{ scope.row.typeRemark }}</template>
            </el-table-column>
            <el-table-column label="类型状态" align="center" prop="typeStatus">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.type_status" :value="scope.row.typeStatus"/>
              </template>               
            </el-table-column>
            <el-table-column label="创建人" width="120">
                <template slot-scope="scope">{{ scope.row.createBy }}</template>
            </el-table-column>
            <el-table-column label="创建时间" width="200">
                <template slot-scope="scope">{{ scope.row.createTime }}</template>
            </el-table-column>

            <el-table-column label="修改人" width="120">
                <template slot-scope="scope">{{ scope.row.updateBy }}</template>
            </el-table-column>
            <el-table-column label="修改时间" width="200">
                <template slot-scope="scope">{{ scope.row.updateTime }}</template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
        </el-table>

        <!-- 添加或修改生成的设备类别对话框 -->
        <el-dialog :title="title" :visible.sync="diaLog" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="类型名称" prop="typeName">
              <el-input v-model="form.typeName" placeholder="请输入类型名称" />
            </el-form-item>
            <el-form-item label="类型说明" prop="typeRemark">
              <el-input v-model="form.typeRemark" placeholder="请输入类型说明" />
            </el-form-item>
            <el-form-item label="类型状态" prop="typeStatus">
              <el-select v-model="form.typeStatus" placeholder="请选择类型状态">
                <el-option
                  v-for="dict in dict.type.type_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
    </div>
  </template>
  
  <script>
  import { listDeviceType, delDeviceType, updateDeviceType, addDeviceType, getDeviceType } from "@/api/device/devicetype";

  
  export default {
    name: "DeviceType",
    dicts: ['type_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        tableData: [],
        // 查询参数
        queryParams: {
            pageNum: 1,
            pageSize: 10,
            typeName: undefined,
            status: undefined
        },
        total: 0,
        form: {},
        diaLog: false,
        title: "",
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询角色列表 */
      getList() {
        listDeviceType(this.queryParams).then(response => {
            this.tableData = response.rows;
            this.total = response.total;
          }
        );
      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.typeId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const typeIds = row.typeId || this.ids;
        this.$modal.confirm('是否确认删除生成的设备类别编号为"' + typeIds + '"的数据项？').then(function() {
          return delDeviceType(typeIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const typeId = row.typeId || this.ids
        getDeviceType(typeId).then(response => {
          this.form = response.data;
          console.log(this.form)
          this.diaLog = true;
          this.title = "修改生成的设备类别";
        });
      },
      // 表单重置
      reset() {
        this.form = {
          typeId: null,
          typeName: null,
          typeRemark: null,
          typeStatus: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      // 取消按钮
      cancel() {
        this.diaLog = false;
        this.reset();
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('devicetype/export', {
          ...this.queryParams
        }, `deviceTypeNew_${new Date().getTime()}.xlsx`)
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.diaLog = true;
        this.title = "添加生成的设备类别";
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.typeId != null) {
              updateDeviceType(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.diaLog = false;
                this.getList();
              });
            } else {
              addDeviceType(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.diaLog = false;
                this.getList();
              });
            }
          }
        });
      },
    }
  };
  </script>